
import {createStore} from "vuex"
import router from "../router";

export default createStore({
    state() {
        // 项目名称
        const projectLogo = {
            Chinese: '到处溜达',
            English: 'WanderAround'
        }
        const baseUrl = 'http://localhost:8899'
        // 导航条
        let navigationIndex = ''
        const orientation = '玉林'
        // 欢迎页搜索关键字
        let searchData = ''
        let busTicketInfo = {
            startPlace:'',
            endPlace:'',
            date:''
        }
        let flightSearchInfo = {
            leaveCity: '',
            arriveCity: '',
            leaveTime: '',
            retraceTime: ''
        }
        let trainSearchInfo = {
            startPlace:'',
            endPlace:'',
            date:'',
            check:true
        }
        function getDate(date) {
            let mDate
            if (date === '') {
                mDate = new Date()
            } else {
                mDate = new Date(date)
            }
            //获取年份
            let Y =mDate.getFullYear();
            //获取月份
            let M = (mDate.getMonth() + 1 < 10 ? '0' + (mDate.getMonth() + 1) : mDate.getMonth() + 1);
            //获取当日日期
            let D = mDate.getDate() < 10 ? '0' + mDate.getDate() : mDate.getDate();

            return Y+'-'+M+'-'+D;
        }

        function getFullTime(date) {
            const mDate = new Date(date)
            //获取年份
            let Y =mDate.getFullYear();
            //获取d月份
            let M = (mDate.getMonth() + 1 < 10 ? '0' + (mDate.getMonth() + 1) : mDate.getMonth() + 1);
            //获取当日日期
            let D = mDate.getDate() < 10 ? '0' + mDate.getDate() : mDate.getDate();
            // 获取小时数
            let H = mDate.getHours() < 10 ? '0' + mDate.getHours() : mDate.getHours();
            // 获取分钟
            let m = mDate.getMinutes() < 10 ? '0' + mDate.getMinutes() : mDate.getMinutes();
            return Y+'-'+M+'-'+D + ' ' + H + ':' + m;
        }
        let user = {
            uid : 0,
            name : '',
            password : '',
            email : '',
            phone : '',
            image : '',
        }

        return {
            searchData,
            busTicketInfo,
            flightSearchInfo,
            navigationIndex,
            trainSearchInfo,
            orientation,
            projectLogo,
            user,
            baseUrl,
            getDate,
            getFullTime
        }
    },
    mutations: {
        // 航班搜索
        flightSearch(state, info){
            state.flightSearchInfo = info
            router.push('/flightSearchResult').then(() =>{
                    console.log("vuex-state: " + state.flightSearchInfo)
                })
        },
        // 汽车票搜索
        busSearch(state, info){
            state.busTicketInfo = info
            router.push('/bus-search-result').then(() => {
                console.log("vuex-state: " + state.busTicketInfo.toString())
                console.log(info.date)
            })
        },
        // 火车票搜索
        trainSearch(state, info) {
            state.trainSearchInfo = info
            router.push('/train-search-result').then(() => {
                console.log("vuex-state: " + state.trainSearchInfo.toString())
                console.log(info.date)
            })
        },
        // 全局搜索
        Search(state, info) {
            state.searchData = info
            router.push('/searchResult').then(() => {
                console.log("vuex-state: " + state.searchData.toString())
                console.log(info)
            })
        },
        navigationChange(state, type) {
            console.log("vuex-curr-state: " + state.navigationIndex)
            if (type === 'plane') {
                state.navigationIndex = '2'
                console.log("vuex-state: " + state.navigationIndex)
                console.log("navigation-type: " + type)
            } else if (type === 'train') {
                state.navigationIndex = '3'
                console.log("vuex-state: " + state.navigationIndex)
                console.log("navigation-type: " + type)
            } else if (type === 'bus') {
                state.navigationIndex = '4'
                console.log("vuex-state: " + state.navigationIndex)
                console.log("navigation-type: " + type)
            } else if (type === 'home') {
                state.navigationIndex = '1'
                console.log("vuex-state: " + state.navigationIndex)
                console.log("navigation-type: " + type)
            } else {
                console.log("vuex : error, 错误的索引类型")
            }
        }

    }
})